(function () {
    window.ucshop = {
        /**
         * API地址前缀
         * @type {string}
         */
        API_PREFIX: "http://datainfo.duapp.com/shopdata/",
        /**
         * 错误码, 0表示成功
         * @type {number}
         */
        CODE: {
            success: 1,//成功
            failed: 2,//失败
            password: 2,//密码错误，格式不对，规定在6-20位数字字母下划线
            exists: 0,//资源已存在
            not_exists: 0//资源不存在
        },
        API_FILE: {
            login_or_register: 'userinfo.php',
            user_info: 'getuser.php',
            cat: 'getclass.php',
            goods: 'getGoods.php',
            ad: 'getBanner.php',
            updatacart: 'updatecar.php',
            cart: 'getCar.php',
            search: 'selectGoodes.php'
        },

        /**
         * 获取API的完整地址
         * @param filename
         * @returns {string}
         */
        getApiAddress: function (filename) {
            return this.API_PREFIX + filename;
        },
        getGoodsCategoryAddress: function () {
            return this.API_PREFIX + this.API_FILE.cat;
        },
        getLoginOrRegisterAddress: function () {
            return this.API_PREFIX + this.API_FILE.login_or_register;
        },
        getUserInfoAddress: function () {
            return this.API_PREFIX + this.API_FILE.user_info;
        },
        getGoodsAddress: function () {
            return this.API_PREFIX + this.API_FILE.goods;
        },
        getPositionAddress: function () {
            return this.API_PREFIX + this.API_FILE.position;
        },
        getAdAddress: function () {
            return this.API_PREFIX + this.API_FILE.ad;
        },
        getCartAddress: function () {
            return this.API_PREFIX + this.API_FILE.cart;
        },
        updataCartAddress: function () {
            return this.API_PREFIX + this.API_FILE.updatacart;
        },
        searchGoodsAddress: function () {
            return this.API_PREFIX + this.API_FILE.search;
        },

        /**
         * 自定义密码加密
         * @param password
         * @returns {string}
         */
        generatePassword: function (password) {
            return md5(password).substr(0, 20);
        },

        /**
         * 保存用户登录信息在本地
         * @param userinfo
         */
        saveLoginedUser: function (data) {
            //存储登录状态到本地,增加记住我的登录状态
            localStorage.clear();
            var userinfo = {};
            userinfo['userID'] = data['userID'];
            userinfo['userimg_url'] = data['userimg_url'];
            userinfo['login_at'] = Date.now();
            userinfo['is_login'] = true;
            for (k in userinfo) {
                localStorage.setItem(k, userinfo[k]);
            }
        },


        /**
         * 检查登录状态
         * @param succ
         * @param fail
         * @returns {*}
         */
        checkLogin: function (succ, fail) {
            var closeTime = 1000 * 3600 * 24 * 7;
            if (localStorage.is_login && (Date.now() - localStorage.login_at < closeTime)) {
                return succ();
            }
            localStorage.clear();
            fail();
        },
        /**
         * 退出
         */
        logout: function () {
            localStorage.clear();
        },
        /**
         * 检查登录状态
         */
        checkLoginStatus: function () {
            //检查登录状态
            this.checkLogin(function () {
                $.getElementsByClassName('login')[0].style.display = 'none';
                $.getElementsByClassName('userinfo')[0].style.display = 'block';
                $.getElementsByClassName('username')[0].innerHTML = localStorage.getItem("userID");
                if (location.pathname.indexOf('cart.html') == -1) {
                    $.getElementsByClassName('toCar')[0].setAttribute("href", "cart.html");
                }


                //如果当前页面是登录或注册页，则自动跳到首页
                console.log(location.pathname);
                if (location.pathname.indexOf('register.html') != -1 || location.pathname.indexOf('login.html') != -1) {
                    location.href = 'index.html';
                }
            }, function () {
                $.getElementsByClassName('login')[0].style.display = 'block';
                $.getElementsByClassName('userinfo')[0].style.display = 'none';
                $.getElementsByClassName('username')[0].innerHTML = "";
                var toCar = $.getElementsByClassName('toCar')[0];
                toCar.setAttribute("href", "javascript:;");
                $.addEvent(toCar, "click", function () {
                    alert("请先登入！");
                })

            });
            //设置退出按钮的事件
            var oLogout = document.getElementById("logout");
            oLogout.addEventListener('click', function () {
                ucshop.logout();
                window.location.href = 'index.html';
            }, false);
        },
        /*
         * 商品收索
         * */
        searchGoods: function () {
            var searchBtn = $.getElementsByClassName("searchBtn")[0];
            var search = document.querySelector("div.search input");
            $.addEvent(searchBtn, "click", function () {

                var search_text = encodeURIComponent(search.value);
                if (search_text) {

                    window.location = "list.html?searchText=" + search_text;
                } else {
                    alert("请输入数据！");
                }

            });
        },
        /*返回顶部*/
        toTop: function () {
            var toTop = $.getElementsByClassName("toTop")[0];
            $.addEvent(toTop, "click", function () {
                var timer = setInterval(function () {
                    var current_top = window.scrollY;
                    var step = parseFloat(current_top / 40);
                    if (current_top > 0) {
                        window.scrollTo(0, current_top - step);
                    } else {
                        clearInterval(timer);
                    }
                }, 4)

            })
        },
        /*吸顶*/
        slide_up: function () {
            var headers = $.getElementsByClassName("header")[0];
            var scroll_befores = 0;
            window.onscroll = function () {
                var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
                if (scroll_befores > scroll_top) {
                    headers.style.height = '50px';
                } else if (scroll_top > 100 && scroll_befores < scroll_top) {
                    headers.style.height = 0;
                }
                scroll_befores = scroll_top;
            }
        },
        /*数据分页*/
        pageShow: function (callback) {
            var pages = 1,
                $pages = $.getElementsByClassName("pages")[0],
                pagesUp = $.children($pages, 0),
                pagesDown = $.children($pages, 2),
                pagesInput = $.children($pages, 1);
            pagesInput.value = pages;
            $.addEvent(pagesDown, "click", function () {
                pagesInput.value++;
                pages = pagesInput.value - 1;
                callback(pages);
            });
            $.addEvent(pagesUp, "click", function () {
                (pagesInput.value) <= 1 ? 1 : (pagesInput.value--);
                pages = pagesInput.value - 1;
                callback(pages);
            });
            $.addEvent(pagesInput, "change", function () {
                pages = pagesInput.value - 1;
                callback(pages);
            });
        },
        /*进度条*/
        progress: function () {
            var pro = document.getElementById("pro");
            var value = pro.value;
            var rangeTime = setInterval(function () {

                value = value > 100?0 :value;
                var step=parseFloat((101-value)/50);
                value+=step;
                pro.value = value;
            }, 6);
            window.rangeTime = rangeTime;
        },
        /*清除进度条*/
        clearRange:function () {
            var pro = $.getElementsByClassName("pro")[0];
           clearInterval(rangeTime) ;
           pro.style.display="none";
        }
    };
})();
